package com.example.development.mysqlStudy.lock;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.development.business.entity.MysqlRowLock;
import com.example.development.business.enums.DeleteFlagEnum;
import com.example.development.business.service.MysqlRowLockService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author: vince_zww
 * @date: 2020/8/24 18:30
 * @desceription:
 */
@Slf4j
@Service
public class LockService {

    private static final Integer ID=8;

    @Autowired
    private MysqlRowLockService service;

    public void save01(){
        for (int i = 0; i < 10; i++) {
            MysqlRowLock mysqlRowLock = MysqlRowLock.builder()
                    .title("标题"+i)
                    .content("内容"+i)
                    .createTime(LocalDateTime.now())
                    .type(1)
                    .deleteFlag(DeleteFlagEnum.FALSE.getCode())
                    .build();
            service.save(mysqlRowLock);
        }

    }

    @Transactional
    public void select01(){
        LambdaQueryWrapper<MysqlRowLock> lamb = new LambdaQueryWrapper();
        lamb.eq(MysqlRowLock::getId,ID);
//        lamb.last("for update");
        MysqlRowLock mysqlRowLock = service.getOne(lamb);
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            log.error("休眠异常");
        }
        log.info("[第一个查询] mysqlRowLock={}",mysqlRowLock);
    }

    @Transactional
    public void select02(){
        LambdaQueryWrapper<MysqlRowLock> lamb = new LambdaQueryWrapper();
        lamb.eq(MysqlRowLock::getId,ID);
//        lamb.last("for update");
        MysqlRowLock mysqlRowLock = service.getOne(lamb);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            log.error("休眠异常");
        }
        log.info("[第二个查询] mysqlRowLock={}",mysqlRowLock);
    }

    @Transactional
    public void select03(){
        LambdaQueryWrapper<MysqlRowLock> lamb = new LambdaQueryWrapper();
        lamb.eq(MysqlRowLock::getId,ID);
//        lamb.last("for update");
        MysqlRowLock mysqlRowLock = service.getOne(lamb);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error("休眠异常");
        }
        log.info("[第三个查询] mysqlRowLock={}",mysqlRowLock);
    }

    @Transactional
    public void update01(){
        MysqlRowLock mysqlRowLock = MysqlRowLock.builder().content("更新08内容").build();
        LambdaQueryWrapper<MysqlRowLock> lamb = new LambdaQueryWrapper();
        lamb.eq(MysqlRowLock::getId,ID);
//        lamb.last("for update");
        Boolean result = service.update(mysqlRowLock,lamb);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            log.error("休眠异常");
        }
        log.info("[第一个更新] result={}",result);
    }

    @Transactional
    public void select04(){
        LambdaQueryWrapper<MysqlRowLock> lamb = new LambdaQueryWrapper();
        lamb.eq(MysqlRowLock::getId,ID);
        lamb.last("for update");
        MysqlRowLock mysqlRowLock = service.getOne(lamb);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error("休眠异常");
        }
        log.info("[第三个查询] mysqlRowLock={}",mysqlRowLock);
    }



}
